package jnpf.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.entity.TemplateEntity;
import jnpf.entity.ThemeStructureVersionEntity;
import jnpf.model.entity.EntityUpTheme;
import jnpf.model.themeLog.Themeupdatelog;
import jnpf.model.znm.theme.tree.TreeDragDTO;
import jnpf.service.ZmnService;
import jnpf.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Tag(name = "themeMange", description = "example")
@RequestMapping("/api/example/znm")
public class ZnmController {
    @Autowired
    private ZmnService znmService;

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Operation(summary = "获取主题树")
    @GetMapping("/getList")
    public ActionResult getThemeTree() {
        return ActionResult.success(znmService.getThemeTree());
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Operation(summary = "获取id")
    @GetMapping("/getId")
    public ActionResult getId() {
        return ActionResult.success(RandomUtil.uuId());
    }

    /**
     * 树拖拽变更
     */
    @Operation(summary = "树拖拽变更")
    @PostMapping("/treeDrag")
    public ActionResult<String> treeDragUpdate(@RequestBody @Valid List<TreeDragDTO> dragData) {
        return znmService.updateTreePosition(dragData);
    }

//    /**
//     * 插入树变更日志
//     */
//    @Operation(summary = "树更新日志")
//    @PostMapping("/treeUpdateLog")
//    public ActionResult<String> treeUpdateLog(@RequestBody @Valid Themeupdatelog dragData) {
//        boolean success = znmService.insertTreeUpdateLog(dragData);
//        return success ? ActionResult.success("节点拖拽日志保存成功") : ActionResult.fail("节点拖拽日志保存异常");
//    }


    @Operation(summary = "导入Excel模板")
    @PostMapping("/importExcelTemplate/{nodeId}")
    public ActionResult<String> importExcelTemplate(@RequestParam("file") MultipartFile file, @PathVariable("nodeId") String nodeId) {
        return znmService.importExcelTemplate(file, nodeId);
    }

    @Operation(summary = "保存主题版本")
    @GetMapping("/saveThemeVersion/{themeId}")
    public ActionResult<String> saveThemeVersion(@PathVariable("themeId") String themeId) {
        return znmService.saveThemeVersion(themeId);
    }

    @Operation(summary = "主题版本查询")
    @GetMapping("/getThemeVersionInfo")
    public ActionResult<List<ThemeStructureVersionEntity>> getThemeVersionInfo() {
        return znmService.getThemeVersionInfo();
    }

    @Operation(summary = "通过id查询主题版本详情")
    @PostMapping("/getThemeVersionByIds")
    public ActionResult<List<ThemeStructureVersionEntity>> getThemeVersion(@RequestBody List<String> themeIds) {
        return znmService.getThemeVersion(themeIds);
    }

    @Operation(summary = "模板创建实体")
    @GetMapping("/createEntity/{templateId}/{count}")
    public ActionResult<String> createEntity(@PathVariable("templateId") @NotEmpty(message = "模板id不能为空") String templateId, @PathVariable("count") @NotEmpty(message = "数量不能能为空") Integer count) {
        return znmService.createEntity(templateId, count);
    }

    @Operation(summary = "实体挂载主题")
    @PostMapping("/entityUpTheme")
    public ActionResult<String> entityUpTheme(@RequestBody @Valid EntityUpTheme entityUpTheme) {
        ArrayList<String> entityIdList = entityUpTheme.getEntityIdList();
        if (entityIdList.isEmpty()) {
            return ActionResult.fail("未选择实体");
        }
        return znmService.entityUpTheme(entityUpTheme);
    }

    @Operation(summary = "查询不同类型的标签列表")
    @GetMapping("/getLabelListByType/{labelType}")
    public ActionResult<List<Map<String, String>>> getLabelListByType(@PathVariable("labelType") String labelType) {
        return znmService.getLabelListByType(labelType);
    }


    @Operation(summary = "查询模板版本列表")
    @GetMapping("/getTemplateVersionList/{templateId}")
    public ActionResult<List<TemplateEntity>> getTemplateVersionList(@PathVariable("templateId") @NotEmpty String templateId) {
        return znmService.getTemplateVersionList(templateId);
    }

    @Operation(summary = "设置模板为主版本")
    @GetMapping("/setMainTemplate/{templateId}")
    public ActionResult<String> setMainTemplate(@PathVariable("templateId") @NotEmpty String templateId) {
        return znmService.setMainTemplate(templateId);
    }

}
